<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-9-1
 * Time: 下午4:09
 * To change this template use File | Settings | File Templates.
 */
class update extends MY_Controller
{
    public function tu_cc()
    {
        $url = 'http://www.2tu.cc/';

        $content = file_get_contents($url);
        $content = iconv('gb2312', 'utf-8//IGNORE', $content);

        preg_match("/最近更新.*<ul.*>(.*)<\/ul>/sU", $content, $codeBlock);
        if (!isset ($codeBlock[1])) {
            echo "no update!\n";exit;
        }
        preg_match_all("/<li>.*<a.*href=\"(.*)\".*>.*<\/li>/sU", $codeBlock[1], $urlTmp);

        if (!isset ($urlTmp[1])) {
            echo "get url error!\n";exit;
        }

        foreach ($urlTmp[1] as $v) {
            preg_match("/GP(.*)\./sU", $v, $sourceId);
            if (empty ($sourceId)) continue;

            $urlArray[$sourceId[1]] = 'http://www.2tu.cc'.$v;
        }

        unset ($urlTmp, $codeBlock);

        if (empty ($urlArray)) {
            echo "no available url!\n";exit;
        }

        $this->load->helper('crawl_tools');
        $config = array('dir' => '/data/m_data/2tucc/');
        $crawl = new crawl_tools($config);
        $crawl->crawlList($urlArray);
        unset ($crawl);

        $path = '/data/m_data/2tucc/';
        $this->load->model('/analysis/tu_cc', 'tuCc');

        foreach ($urlArray as $k=>$v) {
            $fileName = $path.intToPath($k).'index.html';

            echo $k.'--'.$fileName."\n";//continue;

            if (!file_exists($fileName)) continue;

            //echo $fileName;continue;
            $data = array('file_name' => $fileName, 'source_id' => $k);
            $this->tuCc->analysis($data);

            $this->tuCc->save();
        }

        //echo '<pre>';print_r($urlArray); echo $content;exit;
    }

    /**
     * 暂不抓取更新，发现此网站更新频率非常低
     */
    public function ffdy_cc()
    {

    }

    public function iverycd_com()
    {
        $url = 'http://www.iverycd.com/index.php';

        $content = file_get_contents($url);
        //$content = iconv('gb2312', 'utf-8', $content);
        //echo $content;

        preg_match("/最新资源.*<div class=\"page_home_list_left\">(.*)<\/div>/sU", $content, $codeBlock);
        if (!isset ($codeBlock[1])) {
            echo "no update!\n";exit;
        }
        preg_match_all("/<li>.*<\/span><a.*href=\"(.*)\".*>.*<\/li>/sU", $codeBlock[1], $urlTmp);

        if (!isset ($urlTmp[1])) {
            echo "get url error!\n";exit;
        }

        $urlArray = array();
        foreach ($urlTmp[1] as $v) {
            preg_match("/details\/(.*)\//sU", $v, $sourceId);
            if (empty ($sourceId)) continue;

            $urlArray[$sourceId[1]] = $v;
        }

        unset ($urlTmp, $codeBlock);

        if (empty ($urlArray)) {
            echo "no available url!\n";exit;
        }

        $this->load->helper('crawl_tools');
        $config = array('dir' => '/data/m_data/iverycd/');
        $crawl = new crawl_tools($config);
        $crawl->crawlList($urlArray);
        unset ($crawl);

        $path = '/data/m_data/iverycd/';

        $this->load->model('analysis/iverycd_com', 'iverycdCom');

        foreach ($urlArray as $k=>$v) {

            $fileName = $path.intToPath($k).'index.html';
            echo $k.'--'.$fileName."\n";//continue;

            if (!file_exists($fileName)) continue;

            $data = array('file_name' => $fileName, 'source_id' => $k);

            $this->iverycdCom->analysis($data);
            $this->iverycdCom->save();
        }

        //echo '<pre>';print_r($urlArray); echo $content;exit;
    }

    /**
     * 人人影视抓更新
     */
    public function yyets_com()
    {
        $todayHtml = 'http://www.yyets.com/html/today.html';

        $content = file_get_contents($todayHtml);

        if (empty ($content)) return false;

        preg_match_all("/<tr.*>.*<td>.*<td>.*<td><a.*href=\"(.*)\".*>.*<\/tr>/sU", $content, $updateList);
        $updateList = $updateList[1];
        if (empty ($updateList)) return false;

        $urlArray = array();
        foreach ($updateList as $v) {
            $id = str_replace('http://www.yyets.com/php/resource/', '', $v);
            $urlArray[$id] = $v;
        }

        if (empty ($urlArray)) return false;

        $this->load->helper('crawl_tools');
        $config = array('dir' => '/data/m_data/yyets/');
        $crawl = new crawl_tools($config);
        $crawl->crawlList($urlArray);
        unset ($crawl);

        $path = '/data/m_data/yyets/';
        $this->load->model('analysis/yyets_com', 'yyetsCom');
        foreach ($urlArray as $k=>$v) {

            $fileName = $path.intToPath($k).'index.html';
            echo $k.'--'.$fileName."\n";//continue;

            if (!file_exists($fileName)) continue;

            $data = array('file_name' => $fileName, 'source_id' => $k);

            $this->yyetsCom->analysis($data);
            $this->yyetsCom->save();
        }

        //echo '<pre>';print_r($urlArray);//exit;

        //echo $content;
    }

    /**
     * 百度地图
     */
    public function baiduXml()
    {
        $this->load->model('movie');
        $field = 'id, china_name, director, starring, version, language, poster_addr, drama, update_time';
        $data = $this->movie->getMovieList($field, 100, 0, null, 'update_time desc');

        $xmlString = $this->load->view('xml/baidu', array('data' => $data), true);

        file_put_contents(FCPATH.'baidu.xml', $xmlString);
        //echo $xmlString;

    }

    /**
     * 谷歌地图
     */
    public function googleXml()
    {
        $this->load->model('movie');
        $field = 'id, update_time';
        $data = $this->movie->getMovieList($field, 100, 0, null, 'update_time desc');

        $xmlString = $this->load->view('xml/google', array('data' => $data), true);

        file_put_contents(FCPATH.'google.xml', $xmlString);
        //echo $xmlString;
    }

    /**
     * rss订阅
     */
    public function rssXml()
    {
        $this->load->model('movie');
        $field = 'id, china_name, director, starring, poster_addr, drama, update_time';
        $data = $this->movie->getMovieList($field, 100, 0, null, 'update_time desc');

        $xmlString = $this->load->view('xml/rss', array('data' => $data), true);

        file_put_contents(FCPATH.'rss.xml', $xmlString);
        //echo $xmlString;
    }

	/**
     * rss订阅
     */
    public function sitemapXml()
    {
        $this->load->model('movie');
        $field = 'id, china_name, director, starring, poster_addr, drama, update_time';
        $data = $this->movie->getMovieList($field, 100, 0, null, 'update_time desc');
//echo '<pre>';print_r($data);exit;
        $xmlString = $this->load->view('xml/sitemap', array('data' => $data), true);
        //echo $xmlString;
        file_put_contents(FCPATH.'sitemap.xml', $xmlString);
        //echo $xmlString;
    }
}
